# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import json
from pymongo import MongoClient
import pymongo
from decimal import Decimal
city=sys.argv[1]
'''
client=MongoClient("123.57.76.91",27017)
#db=client['admin']
db=client['zhuge_etl']
db.authenticate('zhuge_etl', 'dsoOe823cBq2)dlX')
'''
mongo_uri = "mongodb://zhuge_etl:dsoOe823cBq2)dlX@123.57.76.91:27017/zhuge_etl?authMechanism=SCRAM-SHA-1"
client = MongoClient(mongo_uri)
db=client['zhuge_etl']
borough_price_file="%s/house" % city
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
newitem={}
name_list = {}
with open(borough_price_file, 'w') as f:
    cnt = 1
    result=db['etl_original_data'].find({"etl_house_type":"Sell","etl_city":"beijing"}).limit(50)
    print ','.join(["房源id","楼层","小区名","城区","商圈","房源价格","室","厅","卫","面积","来源平台","抓取时间","原始url","列表页图片url","详情页图片url"])
    for item in result:
        #if item["borough_name"] == "":
        #    continue     
        print ",".join([str(cnt),item["house_floor"],item["borough_name"],item["cityarea_id"],item["cityarea2_id"],item["house_price"],item["house_room"],item["house_hall"],item["house_toilet"],item["house_totalarea"],item["source"],str(item["etl_created_at"]),item["source_url"],"http://img11.soufunimg.com/viewimage/agents/2017_06/10/M16/06/B6/ChCE4Fk7cBqIdcSbAAD_IPAopskAAgGVgDQrbIAAP84065/452x339c.jpg","http://img11.soufunimg.com/viewimage/agents/2017_06/10/M16/06/B6/ChCE4Fk7cBqIdcSbAAD_IPAopskAAgGVgDQrbIAAP84065/452x339c.jpg|http://img11.soufunimg.com/viewimage/agents/2017_06/10/M0B/06/B6/ChCE4Fk7cBiISK4kAADv1dTj_rUAAgGQgMWPo0AAO_t249/452x339c.jpg"])
        cnt = cnt + 1
